package com.hardroid.workload.ui.fragment;

import java.util.ArrayList;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.hardroid.workload.R;
import com.hardroid.workload.utils.EmployerAdapter;
import com.hardroid.workload.utils.Singleton;
import com.hardroid.workload.workcalc.Employer;
import com.hardroid.workload.workcalc.WorkUnit;

public class NewUnitFragment extends ListFragment {

	private ArrayList<Employer> lEmployer;
	private ArrayList<Employer> lUnregisteredEmployer;
	
	
	EmployerAdapter listOfEmployerAdapter;
	ArrayAdapter<Employer> spinnerAdapter;
	private EditText nameText;	
	private EditText unitText;
	private EditText jobText;	
		
	private Button addButton;
	private Button daftarButton;
	private Spinner respondenSpinner;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_new_unit_kerja, container,
				false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		nameText = (EditText) getView().findViewById(
				R.id.new_unit_name_text);		
		unitText = (EditText) getView().findViewById(
				R.id.new_unit_unit_text);
		jobText = (EditText) getView().findViewById(R.id.new_unit_job_text);		
		addButton = (Button) getView().findViewById(
				R.id.new_unit_add_button);		
		daftarButton = (Button) getView().findViewById(
				R.id.new_unit_daftar_button);		
		respondenSpinner = (Spinner) getView().findViewById(
				R.id.new_unit_employer_spinner);		
		
		
		
		lEmployer = new ArrayList<Employer>();
		listOfEmployerAdapter = new EmployerAdapter(getActivity(), lEmployer);
		setListAdapter(listOfEmployerAdapter);
		
//		ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter
//				.createFromResource(getActivity(),
//						R.array.beban_saat_ambil_data,
//						android.R.layout.simple_spinner_item);
//		
//		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		lUnregisteredEmployer = WorkUnit.getListOfUnregisteredEmployer();
		
		System.out.println("List of unregistered Employer "+lUnregisteredEmployer.size());
		
		spinnerAdapter = new ArrayAdapter<Employer>(getActivity(), android.R.layout.simple_spinner_item, lUnregisteredEmployer);
		respondenSpinner.setAdapter(spinnerAdapter);
		
				
		addButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (!nameText.getText().toString().equals("")				
						&& !unitText.getText().toString().equals("")
						&& !jobText.getText().toString().equals("") && !lEmployer.isEmpty()) {
					
					String name = nameText.getText().toString();					
					String unit = unitText.getText().toString();
					String job = jobText.getText().toString();					
					
					Singleton.currentProject.addWorkUnit(name, unit, job,lEmployer);
					
					//commit
					Singleton.unitAdapter.notifyDataSetChanged();					
					boolean saved = Singleton.saveProject(Singleton.currentProject);					
					Toast.makeText(getActivity(), "Successfully add new unit work",
							Toast.LENGTH_SHORT).show();		
					getActivity().finish();
				} else {
					Toast.makeText(getActivity(), "Data are not valid",
							Toast.LENGTH_SHORT).show();

				}
			}
		});
		
		daftarButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println("Daftar Button clicked");
				System.out.println("List of unregistered Employer "+lUnregisteredEmployer.size());
				
				if (!respondenSpinner.getAdapter().isEmpty()){
					int idx = respondenSpinner.getSelectedItemPosition();
					lEmployer.add(lUnregisteredEmployer.get(idx));
					listOfEmployerAdapter.notifyDataSetChanged();
					lUnregisteredEmployer.remove(idx);					
					spinnerAdapter.notifyDataSetChanged();	
																								
				
				}
				
			}
		});
	}
	
	
}